/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.DataSet;

import com.nc.kyiv.yellow.sociallibrary.DataSetDAO.GlobalLibDataSet;
import com.nc.kyiv.yellow.sociallibrary.POJO.Book;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.AbstractDataSet;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Alena
 */
public class GetGlobalLib implements CommonClassesInterface {

    private String userId;

    public GetGlobalLib(String userID) {
        this.userId = userID;
    }
    private String[] mapKeys = {"pageNumber", "booksNumber", "filterOneValue", "filterTwoValue"};
    private MapBuilder helper = new MapBuilder();
    private String resultString = null;

    @Override
    public String execute(String parameters) throws Exception {
        
        Map postParameters = helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());
        if (!userId.equals("guest")) {
            postParameters.put("userId", userId);
        }

        if (postParameters != null) {

            AbstractDataSet globalLibDS = new GlobalLibDataSet();
            List<Book> globalLib = (List<Book>) globalLibDS.getDataSet(postParameters); 
            
            ResultStringOfBooks resultStringOfBooks = new ResultStringOfBooks();
            resultString = resultStringOfBooks.getResultString(globalLib, helper);
            
            if (resultString.length() > 5) {
                resultString = resultString.substring(0, resultString.length() - 5);
            } else {
                return "failed";
            }

            return resultString;

        } else {
            return "failed";
        }
    }
}
